/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package androidx.test.espresso.action;

import static android.view.MotionEvent.ACTION_DOWN;
import static android.view.MotionEvent.ACTION_MOVE;
import static android.view.MotionEvent.ACTION_UP;
import static com.google.common.truth.Truth.assertWithMessage;

import android.os.Build;
import android.view.InputDevice;
import android.view.MotionEvent;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.filters.SmallTest;
import org.junit.Test;
import org.junit.runner.RunWith;

@SmallTest
@RunWith(AndroidJUnit4.class)
public class MotionEventsUnitTest {
  private static final float[] PRECISION = new float[] {1f, 2f};
  private static final float[] COORDINATES = new float[] {10f, 20f};
  private static final float[] COORDINATES_1 = new float[] {30f, 40f};
  private static final int BUTTON_STATE = 12;

  private static final int DEFAULT_BUTTON_STATE = MotionEvent.BUTTON_PRIMARY;
  /**
   * This is the default source for events generated by Espresso. The Android platform's default
   * source for events obtained through {@link MotionEvent#obtain} may be different.
   */
  private static final int DEFAULT_SOURCE = InputDevice.SOURCE_UNKNOWN;

  private static final int DEFAULT_TOOL_TYPE = MotionEvent.TOOL_TYPE_UNKNOWN;

  private static void verifyEvent(
      MotionEvent event,
      int action,
      long downTime,
      float[] coords,
      float[] precision,
      int source,
      int buttonState,
      int toolType) {
    assertWithMessage("not null").that(event).isNotNull();
    assertWithMessage("action").that(event.getAction()).isEqualTo(action);
    assertWithMessage("down time").that(event.getDownTime()).isEqualTo(downTime);
    assertWithMessage("X").that(event.getX()).isEqualTo(coords[0]);
    assertWithMessage("Y").that(event.getY()).isEqualTo(coords[1]);
    assertWithMessage("X precision").that(event.getXPrecision()).isEqualTo(precision[0]);
    assertWithMessage("Y precision").that(event.getYPrecision()).isEqualTo(precision[1]);
    assertWithMessage("source").that(event.getSource()).isEqualTo(source);
    assertWithMessage("button state").that(event.getButtonState()).isEqualTo(buttonState);
    assertWithMessage("tool type").that(event.getToolType(0)).isEqualTo(toolType);
  }

  @Test
  public void testObtainDown() {
    final MotionEvent event = MotionEvents.obtainDownEvent(COORDINATES, PRECISION);
    verifyEvent(
        event,
        ACTION_DOWN,
        event.getEventTime(),
        COORDINATES,
        PRECISION,
        DEFAULT_SOURCE,
        DEFAULT_BUTTON_STATE,
        DEFAULT_TOOL_TYPE);
  }

  @Test
  public void testObtainDownWithCustomSource() {
    final MotionEvent event =
        MotionEvents.obtainDownEvent(
            COORDINATES, PRECISION, InputDevice.SOURCE_MOUSE, BUTTON_STATE);
    verifyEvent(
        event,
        ACTION_DOWN,
        event.getEventTime(),
        COORDINATES,
        PRECISION,
        InputDevice.SOURCE_MOUSE,
        BUTTON_STATE,
        MotionEvent.TOOL_TYPE_MOUSE);
  }

  @Test
  public void testObtainMovement() {
    final MotionEvent down =
        MotionEvents.obtainDownEvent(
            COORDINATES, PRECISION, InputDevice.SOURCE_MOUSE, BUTTON_STATE);
    final MotionEvent move = MotionEvents.obtainMovement(down, COORDINATES_1);
    verifyEvent(
        move,
        ACTION_MOVE,
        down.getEventTime(),
        COORDINATES_1,
        PRECISION,
        InputDevice.SOURCE_MOUSE,
        BUTTON_STATE,
        MotionEvent.TOOL_TYPE_MOUSE);
  }

  @Test
  public void testObtainUp() {
    final MotionEvent down =
        MotionEvents.obtainDownEvent(
            COORDINATES, PRECISION, InputDevice.SOURCE_MOUSE, BUTTON_STATE);
    final MotionEvent up = MotionEvents.obtainUpEvent(down, COORDINATES_1);
    verifyEvent(
        up,
        ACTION_UP,
        down.getEventTime(),
        COORDINATES_1,
        PRECISION,
        InputDevice.SOURCE_MOUSE,
        BUTTON_STATE,
        MotionEvent.TOOL_TYPE_MOUSE);
  }

  /** Regression test for #1458 */
  @Test
  public void testObtainUp_toolTypeFromDownEvent() {
    final MotionEvent down =
        MotionEvents.obtainDownEvent(
            COORDINATES, PRECISION, InputDevice.SOURCE_TOUCHSCREEN, BUTTON_STATE);
    // toolType here will be TOOL_TYPE_FINGER, but lets reset source
    down.setSource(InputDevice.SOURCE_UNKNOWN);
    final MotionEvent up = MotionEvents.obtainUpEvent(down, COORDINATES_1);
    verifyEvent(
        up,
        ACTION_UP,
        down.getEventTime(),
        COORDINATES_1,
        PRECISION,
        InputDevice.SOURCE_UNKNOWN,
        BUTTON_STATE,
        MotionEvent.TOOL_TYPE_FINGER);
  }

  /** Regression test for #1840 */
  @Test
  @SuppressWarnings("deprecation") // Intentionally testing a deprecated but supported method.
  public void testObtainMovement_deprecated() {
    final MotionEvent move = MotionEvents.obtainMovement(100L, 100L, COORDINATES);
    var expectedSource =
        (Build.VERSION.SDK_INT < Build.VERSION_CODES.S_V2)
            ? InputDevice.SOURCE_CLASS_NONE
            : InputDevice.SOURCE_CLASS_POINTER;
    verifyEvent(
        move,
        ACTION_MOVE,
        100L, // Arbitrary value we set when calling obtainMovement.
        COORDINATES,
        new float[] {
          1f, 1f
        }, // Since we are evaluating obtainMovement we have the default precision.
        expectedSource, // obtainMovement defaults to this.
        MotionEvent.CLASSIFICATION_NONE, // The buttonState defaults to this.
        MotionEvent.CLASSIFICATION_NONE); // The toolType defaults to this.
  }
}
